package com.xinzhi.controller;

import com.xinzhi.anno.CheckRole;
import com.xinzhi.domain.Man;
import com.xinzhi.domain.User;
import com.xinzhi.service.LoginService;
import com.xinzhi.utils.Result;
import com.xinzhi.service.UserService;
import com.xinzhi.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

import static com.xinzhi.utils.lsJWTUtils.checkToken;

@CrossOrigin
@Controller
@ResponseBody
@RequestMapping("/user")
public class UserController {
    @Autowired
   private UserService userService;
    @Autowired
    private LoginService loginService;
    @CheckRole(role = {"user"})
    @RequestMapping("/getinfo")
    public Result getUserInfo(HttpServletRequest request){
//        boolean token = JwtUtil.checkToken(request.getHeader("token"));
        User token = loginService.checkToken(request.getHeader("token"));
//        if (token.equals(null)) return Result.error(5010,"tooken无效");
        return userService.getInfo(token.getUserId());
    }
    @CheckRole(role = {"user"})
    @RequestMapping("/uptname")
    @ResponseBody
    public Result uptname(String tname,HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
//        if (token.equals(null)) return Result.error(5010,"tooken无效");
        return userService.uptname(tname, token.getUserId());

    }
    @CheckRole(role = {"user"})
    @RequestMapping("/recharge")
    @ResponseBody
    public Result recharge(BigDecimal money, HttpServletRequest request) {
        User token = loginService.checkToken(request.getHeader("token"));
//        if (token.equals(null)) return Result.error(5010,"tooken无效");
        if (!money.equals(null)) {
            return userService.recharge(token.getUserId(), money);
        } else {
            return Result.error(5008, "输入值不能为空");
        }
    }
    //    @RequestMapping(value = "/upheadhost",method = RequestMethod.POST)
//    @ResponseBody
//    public Result uptheadhost(String headhost, int id, HttpServletRequest request){
//        return iuserInfoService.upheadhost(headhost, id, request);
//    }
    @CheckRole(role = {"user"})
    @RequestMapping("/inMan")
    @ResponseBody
    public Result inMan(@RequestBody Man man, HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
//        if (token.equals(null)) return Result.error(5010,"tooken无效");
        return userService.inMan(token.getUserId(), man.getName(),man.getPhone()
        );
    }
    @CheckRole(role = {"user"})
    @RequestMapping("/getMan")
    @ResponseBody
    public Result getMan(HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
//        if (token.equals(null)) return Result.error(5010,"tooken无效");
        //查询该用户id所有的自提人信息

        return userService.getMan(token.getUserId());
    }
    @CheckRole(role = {"user"})
    @RequestMapping("/deMan")
    @ResponseBody
    public Result deMan(HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
        if (token.equals(null)) return Result.error(5010,"tooken无效");
        //拿到这条信息的id用来删除此条信息
        return userService.deMan(token.getUserId());
    }
    @CheckRole(role = {"user"})
    @RequestMapping("/upMan")
    @ResponseBody
    public Result upMan(String name,String phone,HttpServletRequest request){
        User token = loginService.checkToken(request.getHeader("token"));
//        if (token.equals(null)) return Result.error(5010,"tooken无效");
        //拿到前端给的id、name、phone来修改信息
        return userService.upMan(token.getUserId(),name,phone);
    }
    @GetMapping("currentUser")
    public Result currentUser(@RequestHeader("Authorization") String token){
        return Result.ok(userService.findUserByToken(token));
    }

}
